﻿<template>
  <div class="app-container">
    <div class="filter-title">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('merchant.id')+' :'">
          <el-input
            v-model="listQuery.merchant_id"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('merchant.id')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('merchant.name')+' :'">
          <el-input
            v-model="listQuery.merchant_name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('merchant.name')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('equipment.ip')+' :'">
          <el-input
            v-model="listQuery.ip"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('equipment.ip')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >{{ $t('table.search') }}</el-button>
        <el-button
          v-if="permission.import"
          style="margin-left: 10px;float:right"
          class="filter-item"
          type="primary"
          icon="el-icon-edit"
          @click="handleImport"
        >{{ $t('table.import') }}</el-button>
        <el-button
          style="margin-left: 10px"
          class="filter-item"
          type="primary"
          @click="handleRefresh"
        >{{ $t('table.refresh') }}</el-button>
        <div v-show="!showSearchItem" class="showDown" @click="showSearchItem = true">
          <i class="el-icon-d-arrow-right"></i>
        </div>
        <div v-show="showSearchItem" class="showUp" @click="showSearchItem = false">
          <i class="el-icon-d-arrow-left"></i>
        </div>
      </el-form>
    </div>
    <div v-show="showSearchItem" class="filter-container">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('equipment.code')+' :'">
          <el-input
            v-model="listQuery.code"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('equipment.code')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('equipment.type')+' :'">
          <el-select
            v-model="listQuery.equipment_type"
            clearable
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('equipment.type')"
          >
            <el-option
              v-for="item in typeOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('public.status')+' :'">
          <el-select
            v-model="listQuery.status"
            clearable
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.status')"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      fixed="left"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255,255,255,0.2)"
      border
      fit
      highlight-current-row
      style="width: 100%min-height:1000px"
    >
      <el-table-column fixed="left" align="center" :label="$t('table.id')" width="65">
        <template slot-scope="scope">
          <span>{{ (listQuery.page-1)*listQuery.limit+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('equipment.name')">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('equipment.code')">
        <template slot-scope="scope">
          <span>{{ scope.row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('merchant.id')">
        <template slot-scope="scope">
          <span>{{ scope.row.merchant_id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('merchant.name')">
        <template slot-scope="scope">
          <span>{{ scope.row.merchant_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('equipment.ip')">
        <template slot-scope="scope">
          <span>{{ scope.row.ip }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('equipment.type')">
        <template slot-scope="scope">
          <span>{{ equipmentTypeEs(scope.row.equipment_type) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('screen.type')">
        <template slot-scope="scope">
          <span>{{ scope.row.screen_attr_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('circle.region')">
        <template slot-scope="scope">
          <span>{{ scope.row.circle_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('equipment.sumEarnings')">
        <template slot-scope="scope">
          <span>{{ scope.row.sum_earnings }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" :label="$t('equipment.status')">
        <template slot-scope="scope">
          <span>{{ switchEsSecond(scope.row.equipment_status) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('public.createTime')">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('public.status')">
        <template slot-scope="scope">
          <span
            :class="statusColorSecond(scope.row.status)"
          >{{ statusEstimation(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="permission.disable"
        align="center"
        :label="$t('operations.cameraStatus')"
        width="150px"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.camera_status"
            active-text="开"
            inactive-text="关"
            :active-value="2"
            :inactive-value="1"
            @change="handleOpen(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        :label="$t('table.actions')"
        :width="operWidth"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleRead(scope.row)"
          >{{ $t('table.read') }}</el-button>
          <el-button
            v-if="scope.row.status === 2 && permission.disable"
            type="danger"
            size="mini"
            @click="handleDisable(scope.row)"
          >{{ $t('table.disable') }}</el-button>
          <el-button
            v-else-if="permission.disable"
            type="success"
            size="mini"
            @click="handleEnable(scope.row)"
          >{{ $t('table.enable') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <!-- 查看 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormRead">
      <el-form
        :model="temp"
        label-position="right"
        label-width="40%"
        style="width: 400px margin-left:120px"
        size="small"
      >
        <el-form-item :label="$t('equipment.name')+' :'">
          <span>{{ temp.name }}</span>
        </el-form-item>
        <el-form-item :label="$t('operations.code')+' :'">
          <span>{{ temp.code }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.referenceName')+' :'">
          <span>{{ temp.description }}</span>
        </el-form-item>
        <el-form-item :label="$t('merchant.name')+' :'">
          <span>{{ temp.merchant_name }}</span>
        </el-form-item>
        <el-form-item v-if="temp.merchant_id" :label="$t('circle.name')+' :'">
          <span>{{ temp.circle_name }}</span>
        </el-form-item>
        <el-form-item :label="$t('equipment.type')+' :'">
          <span>{{ equipmentTypeEs(temp.equipment_type) }}</span>
        </el-form-item>
        <el-form-item v-if="temp.screen_attr_name" :label="$t('screen.type')+' :'">
          <span>{{ temp.screen_attr_name }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.charge')+' :'">
          <span>{{ temp.charge }}</span>
        </el-form-item>
        <el-form-item :label="$t('equipment.ip')+' :'">
          <span>{{ temp.ip }}</span>
        </el-form-item>
        <el-form-item :label="$t('equipment.installName')+' :'">
          <span>{{ temp.install_name }}</span>
        </el-form-item>
        <el-form-item :label="$t('equipment.installMobile')+' :'">
          <span>{{ temp.install_mobile }}</span>
        </el-form-item>
        <el-form-item v-if="temp.maintain_name" :label="$t('equipment.maintainName')+' :'">
          <span>{{ temp.maintain_name }}</span>
        </el-form-item>
        <el-form-item v-if="temp.maintain_mobile" :label="$t('equipment.maintainMobile')+' :'">
          <span>{{ temp.maintain_mobile }}</span>
        </el-form-item>
        <el-form-item :label="$t('equipment.installSite')+' :'">
          <span>{{ temp.install_site }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.status')+' :'">
          <span>{{ statusEstimation(temp.status) }}</span>
        </el-form-item>
        <el-form-item v-if="temp.status === 3" :label="$t('public.auditOpinion')+' :'">
          <span>{{ temp.remark }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "EquipmentList",
  data() {
    return {
      showSearchItem: false,
      tableKey: 0,
      list: null,
      total: null,
      regionType: null,
      regionId: null,
      listLoading: true,
      statusOptions: [
        {
          value: 1,
          label: "审核中"
        },
        {
          value: 2,
          label: "审核通过"
        },
        {
          value: 3,
          label: "未通过"
        },
        {
          value: 4,
          label: "禁用"
        }
      ],
      typeOptions: [],
      listQuery: {
        page: 1,
        limit: 20,
        merchant_id: "",
        merchant_name: "",
        ip: "",
        status: "",
        code: "",
        equipment_type: ""
      },
      temp: {
        id: undefined,
        pid: "",
        name: "",
        linkman: "",
        linkman_mobile: "",
        region_id: "",
        region_name: "",
        region_type: ""
      },
      dialogFormVisible: false,
      dialogFormRead: false,
      dialogStatus: "",
      textMap: {
        edit: "编辑",
        create: "添加",
        review: "审核",
        read: "查看"
      },
      createDisabled: true,
      updateDisabled: true,
      loading: false,
      merchantOptions: [],
      permission: {
        disable: false,
        import: false
      }
    };
  },
  computed: {
    operWidth: function() {
      let itemCount = 1;
      if (this.permission.disable) {
        itemCount += 1;
      }
      return itemCount * 80 + "px";
    }
  },
  created() {
    if (this.$store.state.permission.jurisdiction === "*") {
      this.permission.disable = true;
      this.permission.import = true;
    } else {
      const jurisdiction = this.$store.state.permission.jurisdiction.split(",");
      jurisdiction.forEach(element => {
        const id = parseInt(element);
        if (id === 138) {
          this.permission.disable = true;
        } else if (id === 226) {
          this.permission.import = true;
        }
      });
    }
    this.regionId = this.$store.state.user.id;
    this.regionType = this.$store.state.user.regionType;
    this.getList();
    this.getEquipmentType();
  },
  methods: {
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page
      };
      if (this.listQuery.merchant_id) {
        data["merchant_id"] = this.listQuery.merchant_id;
      }
      if (this.listQuery.merchant_name) {
        data["merchant_name"] = this.listQuery.merchant_name;
      }
      if (this.listQuery.ip) {
        data["ip"] = this.listQuery.ip;
      }
      if (this.listQuery.status) {
        data["status"] = this.listQuery.status;
      }
      if (this.listQuery.code) {
        data["code"] = this.listQuery.code;
      }
      if (this.listQuery.equipment_type) {
        data["equipment_type"] = this.listQuery.equipment_type;
      }
      const equipmentListPath = {
        // 获取智能广告终端列表
        company: "STQ",
        api: "pc_equipment_list",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(equipmentListPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    resetArea() {
      this.areaOptions = [];
      this.temp.region_id = "";
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        merchant_id: "",
        merchant_name: "",
        ip: "",
        status: "",
        code: "",
        equipment_type: ""
      };
      this.getList();
    },
    handleImport(row) {
      // 批量导入智能终端
      this.$router.push({
        name: "upload/index"
      });
    },
    handleRead(row) {
      // 打开查看弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "read";
      this.dialogFormRead = true;
    },
    handleDisable(row) {
      // 禁用智能终端
      const itocEditPath = {
        company: "STQ",
        api: "pc_equipment_editEquipment",
        version: "1.0",
        data: {
          id: row.id,
          status: 4
        }
      };
      this.socketApi.sendSock(JSON.stringify(itocEditPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleEnable(row) {
      // 启用智能终端
      const itocEditPath = {
        company: "STQ",
        api: "pc_equipment_editEquipment",
        version: "1.0",
        data: {
          id: row.id,
          status: 2
        }
      };
      this.socketApi.sendSock(JSON.stringify(itocEditPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleOpen(row) {
      // 打开调试模式
      const cameraStatusPath = {
        company: "STQ",
        api: "pc_equipment_editEquipment",
        version: "1.0",
        data: {
          id: row.id,
          camera_status: row.camera_status
        }
      };
      this.socketApi.sendSock(JSON.stringify(cameraStatusPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 商家选择
    getMerchant(query) {
      const merchantGetPath = {
        company: "STQ",
        api: "pc_merchant_getName",
        version: "1.0",
        data: {
          name: query
        }
      };
      this.socketApi.sendSock(JSON.stringify(merchantGetPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 获取设备类型
    getEquipmentType() {
      const merchantGetPath = {
        company: "STQ",
        api: "pc_equipmentType_index",
        version: "1.0",
        data: {}
      };
      this.socketApi.sendSock(JSON.stringify(merchantGetPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "pc_equipment_list") {
            this.total = resj.count;
            this.list = resj.data;
            this.listLoading = false;
          } else if (resj.api === "pc_merchant_getName") {
            this.merchantOptions = resj.data;
          } else if (resj.api === "pc_equipment_editEquipment") {
            this.getList();
            this.$notify({
              title: "成功",
              message: "更新成功",
              type: "success",
              duration: 2000
            });
          } else if (resj.api === "pc_equipmentType_index") {
            this.typeOptions = resj.data;
          }
        }
      }
    },
    equipmentTypeEs(type) {
      let equipmentTypeName = "";
      this.typeOptions.forEach(element => {
        if (element.id === parseInt(type)) {
          equipmentTypeName = element.name;
        }
      });
      return equipmentTypeName;
    }
  }
};
</script>
